package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.CollectDish;
import com.tjetc.model.CollectDishDetail;
import com.tjetc.service.CollectDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("collectDish")
public class CollectDishController {
    @Autowired
    private CollectDishService collectDishService;

    /***
     * 添加用户收藏的菜品
     * @param collectDish
     * @return
     */
    @RequestMapping("add")
    private JsonResult addCollectDish(CollectDish collectDish) {
        int row = collectDishService.addDish(collectDish);
        JsonResult result = new JsonResult<>(0, "", row);
        return result;
    }

    /***
     * 删除用户收藏的菜品
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    private JsonResult deleteCollectDish(@PathVariable Integer id) {
        int row = collectDishService.deleteDish(id);
        if (row == 0) return new JsonResult(0, "菜品不存在", "");
        return new JsonResult(1, "菜品删除成功", row);
    }

    /***
     * 获取用户收藏的所有菜品
     * @param userId
     * @return
     */
    @RequestMapping("all/{id}")
    private JsonResult getAll(@PathVariable("id") Integer userId) {
        System.out.println(userId);
        List<CollectDishDetail> dishes = collectDishService.selectByUser(userId);
        if (dishes != null) {
            return new JsonResult(0, "查询成功", dishes);
        } else {
            return new JsonResult(0, "查询失败，不存在收藏菜品", dishes);
        }
    }

}
